<?php
namespace app\index\controller;
use think\Cookie;
use think\Session;
use think\Loader;
/**
 * 用户
 */
class User extends Api
{
	public function getIndex(){
		if(model('Users')->loginByOpendId($this->opend_id)){
			$this->redirect('/article/list');
			return ;
		}
		return $this->fetch('login');		
	}
	
	public function postLogin(){
		$re = model('Users')->login($this->request->post());
		return json($re);
	}
	
	/**
	 * 退出登录
	 */
	public function getLogout(){
		model('Users')->where(['id'=>Session::get('wx_user.id')])->setField('opend_id', '');
		Cookie::delete('auth_id');
		Session::delete('wx_user');
		return $this->fetch('login');		
	}
	
	/**
	 * 获取授权用户信息
	 */
	public function getCallback(){
	   	$user = $this->app->oauth->user();
		Cookie::forever('auth_id', $user->getId());
		$this->redirect('/user/index');
	}
	
	/**
	 * 用户注册表单
	 */
	public function getRegister(){
		return $this->fetch('register');		
	}
	
	/**
	 * 用户注册
	 */
	public function postRegister(){
		if(!captcha_check($this->request->post('imgCode'))){
			return json(['code' => 'imgCode', 'msg' => '图像验证码错误']);
		}
		if($this->request->post('password') != $this->request->post('confirmPwd')){
			return json(['code' => 'cfpwd', 'msg' => '两次输入的密码不一致']);
		}
	    $re = Loader::model('Users') -> addUser($this->request->post());
		return json($re);
	}
} 
 
 
 
 
?>